package com.zcm.modules.xgb.utils;

import com.google.gson.Gson;
import com.zcm.common.config.Constant;
import com.zcm.common.utils.SpringContextHolder;
import com.zcm.common.utils.StringUtils;
import com.zcm.modules.xgb.entity.*;
import com.zcm.modules.xgb.service.*;
import com.zcm.modules.zhaocai.entity.*;

import java.util.*;

public class XgbUtils {

    private static XgbBoardStockService xgbBoardStockService = SpringContextHolder.getBean(XgbBoardStockService.class);
    private static XgbBoardDownService xgbBoardDownService = SpringContextHolder.getBean(XgbBoardDownService.class);
    private static XgbRealService xgbRealService = SpringContextHolder.getBean(XgbRealService.class);
    private static XgbBoardPredayService xgbBoardPredayService = SpringContextHolder.getBean(XgbBoardPredayService.class);
    private static XgbBoomStockService xgbBoomStockService = SpringContextHolder.getBean(XgbBoomStockService.class);
    private static XgbMultiStockService xgbMultiStockService = SpringContextHolder.getBean(XgbMultiStockService.class);
    private static XgbQuoteChangeService xgbQuoteChangeService = SpringContextHolder.getBean(XgbQuoteChangeService.class);

    static public void parseXgbData(ZcmKanpanData zcmKanpanData){
        Gson gson = new Gson();
        if(zcmKanpanData==null){
            return;
        }

        if("boardStockTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();
            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }
            if(!validBoardStock(titleIndex)){
                return;
            }
            for (List<Object> item : data.getItems()) {
                String symbol = item.get(titleIndex.get("symbol")).toString();
                String reason = item.get(titleIndex.get("reason")).toString();
                symbol = symbol.replace(".SS","").replace(".SZ","");
                if(StringUtils.isEmpty(reason)) {
                    reason = "请关注微信" + Constant.DUODUO_MPACCOUNT;
                }else{
                    reason = reason.replace("选股宝", Constant.DUODUO_MPACCOUNT).replace("脱水研报", Constant.DUODUO_MPACCOUNT);
                }
                XgbBoardStock xgbBoardStock = new XgbBoardStock();
                xgbBoardStock.setSymbol(symbol);
                xgbBoardStock.setTitle(item.get(titleIndex.get("title")).toString());
                xgbBoardStock.setReason(reason);
                xgbBoardStock.setCurPrice((Double) item.get(titleIndex.get("cur_price")));
                xgbBoardStock.setPxChangeRate((Double) item.get(titleIndex.get("px_change_rate")));
                xgbBoardStock.setBlockadeRatio((Double) item.get(titleIndex.get("blockade_ratio")));
                xgbBoardStock.setTurnoverRatio((Double) item.get(titleIndex.get("turnover_ratio")));
                xgbBoardStock.setCirculationValue((Double) item.get(titleIndex.get("circulation_value")));
                xgbBoardStock.setTotalValue((Double) item.get(titleIndex.get("total_value")));
                xgbBoardStock.setFirstRaiseTime(((Double)item.get(titleIndex.get("first_raise_time"))).intValue());
                xgbBoardStock.setLastRaiseTime(((Double)item.get(titleIndex.get("last_raise_time"))).intValue());
                xgbBoardStock.setOpenCount(((Double)item.get(titleIndex.get("open_count"))).intValue());
                xgbBoardStock.setContinueBoardCount(((Double)item.get(titleIndex.get("continue_board_count"))).intValue());
                // xgbBoardStock.setBoolNewStock(item.get(titleIndex.get("bool_new_stock")));
                xgbBoardStock.setOriginId(zcmKanpanData.getId());
                xgbBoardStock.setDay(zcmKanpanData.getDay());
                xgbBoardStockService.save(xgbBoardStock);
            }
        }else if("boomStockTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();
            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }

            if(!validBoomStock(titleIndex)){
                return;
            }
            for (List<Object> item : data.getItems()) {
                String symbol = item.get(titleIndex.get("symbol")).toString();
                String reason = item.get(titleIndex.get("reason")).toString();
                symbol = symbol.replace(".SS","").replace(".SZ","");
                if(StringUtils.isEmpty(reason)) {
                    reason = "请关注微信" + Constant.DUODUO_MPACCOUNT;
                }else{
                    reason = reason.replace("选股宝", Constant.DUODUO_MPACCOUNT).replace("脱水研报", Constant.DUODUO_MPACCOUNT);
                }
                XgbBoomStock xgbBoomStock = new XgbBoomStock();
                xgbBoomStock.setSymbol(symbol);
                xgbBoomStock.setTitle(item.get(titleIndex.get("title")).toString());
                xgbBoomStock.setReason(reason);
                xgbBoomStock.setCurPrice((Double) item.get(titleIndex.get("cur_price")));
                xgbBoomStock.setPxChangeRate((Double) item.get(titleIndex.get("px_change_rate")));
                xgbBoomStock.setLastBoomTime(((Double)item.get(titleIndex.get("last_boom_time"))).intValue());
                xgbBoomStock.setTurnoverRatio((Double) item.get(titleIndex.get("turnover_ratio")));
                xgbBoomStock.setCirculationValue((Double) item.get(titleIndex.get("circulation_value")));
                xgbBoomStock.setTotalValue((Double) item.get(titleIndex.get("total_value")));
                xgbBoomStock.setFirstRaiseTime(((Double)item.get(titleIndex.get("first_raise_time"))).intValue());
                xgbBoomStock.setLastRaiseTime(((Double)item.get(titleIndex.get("last_raise_time"))).intValue());
                xgbBoomStock.setOpenCount(((Double)item.get(titleIndex.get("open_count"))).intValue());
                xgbBoomStock.setContinueBoardCount(((Double)item.get(titleIndex.get("continue_board_count"))).intValue());
                xgbBoomStock.setOriginId(zcmKanpanData.getId());
                xgbBoomStock.setDay(zcmKanpanData.getDay());
                xgbBoomStockService.save(xgbBoomStock);
            }
        }else if("boardDownTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();

            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }


            if(!validBoardDown(titleIndex)){
                return;
            }
            for (List<Object> item : data.getItems()) {
                String symbol = item.get(titleIndex.get("symbol")).toString();
                String reason = item.get(titleIndex.get("reason")).toString();
                symbol = symbol.replace(".SS","").replace(".SZ","");
                if(StringUtils.isEmpty(reason)) {
                    reason = "请关注微信" + Constant.DUODUO_MPACCOUNT;;
                }else{
                    reason = reason.replace("选股宝", Constant.DUODUO_MPACCOUNT).replace("脱水研报", Constant.DUODUO_MPACCOUNT);
                }
                XgbBoardDown xgbBoardDown = new XgbBoardDown();
                xgbBoardDown.setSymbol(symbol);
                xgbBoardDown.setTitle(item.get(titleIndex.get("title")).toString());
                xgbBoardDown.setReason(reason);
                xgbBoardDown.setCurPrice((Double) item.get(titleIndex.get("cur_price")));
                xgbBoardDown.setPxChangeRate((Double) item.get(titleIndex.get("px_change_rate")));
                xgbBoardDown.setBlockadeRatio((Double) item.get(titleIndex.get("blockade_ratio")));
                xgbBoardDown.setTurnoverRatio((Double) item.get(titleIndex.get("turnover_ratio")));
                xgbBoardDown.setCirculationValue((Double) item.get(titleIndex.get("circulation_value")));
                xgbBoardDown.setTotalValue(((Double) item.get(titleIndex.get("total_value"))));
                xgbBoardDown.setFirstBoardTime(((Double)item.get(titleIndex.get("first_board_time"))).intValue());
                xgbBoardDown.setLastBoardTime(((Double)item.get(titleIndex.get("last_board_time"))).intValue());
                xgbBoardDown.setOpenCount(((Double)item.get(titleIndex.get("open_count"))).intValue());
                xgbBoardDown.setContinueBoardCount(((Double)item.get(titleIndex.get("continue_board_count"))).intValue());
                xgbBoardDown.setOriginId(zcmKanpanData.getId());
                xgbBoardDown.setDay(zcmKanpanData.getDay());
                xgbBoardDownService.save(xgbBoardDown);

            }
        }else if("boardPredayTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();

            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }

            if(!validBoardPreday(titleIndex)){
                return;
            }

            for (List<Object> item : data.getItems()) {
                String symbol = item.get(titleIndex.get("symbol")).toString();
                String reason = item.get(titleIndex.get("reason")).toString();
                symbol = symbol.replace(".SS","").replace(".SZ","");
                if(StringUtils.isEmpty(reason)) {
                    reason = "请关注微信" + Constant.DUODUO_MPACCOUNT;
                }else{
                    reason = reason.replace("选股宝", Constant.DUODUO_MPACCOUNT).replace("脱水研报", Constant.DUODUO_MPACCOUNT);
                }
                XgbBoardPreday xgbBoardPreday = new XgbBoardPreday();
                xgbBoardPreday.setSymbol(symbol);
                xgbBoardPreday.setTitle(item.get(titleIndex.get("title")).toString());
                xgbBoardPreday.setReason(reason);
                xgbBoardPreday.setCurPrice((Double) item.get(titleIndex.get("cur_price")));
                xgbBoardPreday.setPxChangeRate((Double) item.get(titleIndex.get("px_change_rate")));
                xgbBoardPreday.setBlockadeRatio((Double) item.get(titleIndex.get("blockade_ratio")));
                xgbBoardPreday.setTurnoverRatio((Double) item.get(titleIndex.get("turnover_ratio")));
                xgbBoardPreday.setCirculationValue((Double) item.get(titleIndex.get("circulation_value")));
                xgbBoardPreday.setTotalValue((Double ) item.get(titleIndex.get("total_value")));
                xgbBoardPreday.setFirstRaiseTime(((Double)item.get(titleIndex.get("first_raise_time"))).intValue());
                xgbBoardPreday.setLastRaiseTime(((Double)item.get(titleIndex.get("last_raise_time"))).intValue());
                xgbBoardPreday.setOpenCount(((Double)item.get(titleIndex.get("open_count"))).intValue());
                xgbBoardPreday.setContinueBoardCount(((Double)item.get(titleIndex.get("continue_board_count"))).intValue());
                //xgbBoardPreday.setBoolNewStock(item.get(titleIndex.get("bool_new_stock")));
                xgbBoardPreday.setOriginId(zcmKanpanData.getId());
                xgbBoardPreday.setDay(zcmKanpanData.getDay());
                xgbBoardPredayService.save(xgbBoardPreday);

            }
        }else if("multiStockTask".equals(zcmKanpanData.getType())){
            DDSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDSourceEntity.class);
            DDTableDataEntity data = entity.getData();

            Map<String,Integer> titleIndex = new HashMap();
            int i = 0;
            for (String field : data.getFields()) {
                titleIndex.put(field,i++);
            }


            if(!validMultiStock(titleIndex)){
                return;
            }
            for (List<Object> item : data.getItems()) {
                String symbol = item.get(titleIndex.get("symbol")).toString();
                String reason = item.get(titleIndex.get("reason")).toString();
                symbol = symbol.replace(".SS","").replace(".SZ","");
                if(StringUtils.isEmpty(reason)) {
                    reason = "请关注微信" + Constant.DUODUO_MPACCOUNT;
                }else{
                    reason = reason.replace("选股宝", Constant.DUODUO_MPACCOUNT).replace("脱水研报", Constant.DUODUO_MPACCOUNT);
                }
                XgbMultiStock xgbMultiStock = new XgbMultiStock();
                xgbMultiStock.setSymbol(symbol);
                xgbMultiStock.setTitle(item.get(titleIndex.get("title")).toString());
                xgbMultiStock.setReason(reason);
                xgbMultiStock.setCurPrice((Double) item.get(titleIndex.get("cur_price")));
                xgbMultiStock.setPxChangeRate((Double) item.get(titleIndex.get("px_change_rate")));
                xgbMultiStock.setTurnoverRatio((Double) item.get(titleIndex.get("turnover_ratio")));
                xgbMultiStock.setCirculationValue((Double) item.get(titleIndex.get("circulation_value")));
                xgbMultiStock.setTotalValue((Double) item.get(titleIndex.get("total_value")));
                xgbMultiStock.setBoardDays(((Double)item.get(titleIndex.get("board_days"))).intValue());
                xgbMultiStock.setBoardCount(((Double)item.get(titleIndex.get("board_count"))).intValue());
                xgbMultiStock.setContinueBoardCount(((Double)item.get(titleIndex.get("continue_board_count"))).intValue());
                xgbMultiStock.setOriginId(zcmKanpanData.getId());
                xgbMultiStock.setDay(zcmKanpanData.getDay());
                xgbMultiStockService.save(xgbMultiStock);
            }

        }else if("realTask".equals(zcmKanpanData.getType())){
            DDRealSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDRealSourceEntity.class);
            Map<String,Object> data = (Map)entity.getData();
            Map<String,List<Object> > snapshot = (Map)data.get("snapshot");

            for (Map.Entry<String, List<Object> > m : snapshot.entrySet()) {
                if("fields".equals(m.getKey())){
                    continue;
                }
                String symbol = m.getKey();
                Double lastPx = (Double) m.getValue().get(0);
                Double pxChangeRate = (Double) m.getValue().get(1);
                XgbReal real = new XgbReal();
                real.setLastPx(lastPx);
                real.setPxChangeRate(pxChangeRate);
                real.setSymbol(symbol);
                real.setOriginId(zcmKanpanData.getId());
                real.setDay(zcmKanpanData.getDay());
                xgbRealService.save(real);
            }
        }else if("quoteChangeTask".equals(zcmKanpanData.getType())){
            DDQuoteSourceEntity entity = gson.fromJson(zcmKanpanData.getContent(),DDQuoteSourceEntity.class);
            Map<String, Object> data = entity.getData();
            XgbQuoteChange xgbQuoteChange = new XgbQuoteChange();
            xgbQuoteChange.setOriginId(zcmKanpanData.getId());
            xgbQuoteChange.setDay(zcmKanpanData.getDay());

            xgbQuoteChange.setDeclineLimitCount(((Double)data.get("decline_limit_count")).intValue());
            xgbQuoteChange.setDeclineNumCount(((Double)data.get("decline_num_count")).intValue());
            xgbQuoteChange.setHaltStocksCount(((Double)data.get("halt_stocks_count")).intValue());
            //xgbQuoteChange.setNormalQuoteCount(data.get("normal_quote_count"));
            xgbQuoteChange.setStDeclineLimitCount(((Double)data.get("st_decline_limit_count")).intValue());
            xgbQuoteChange.setSurgedLimitCount(((Double)data.get("st_surged_limit_count")).intValue());
            xgbQuoteChange.setStableStocksCount(((Double)data.get("stable_stocks_count")).intValue());
            xgbQuoteChange.setStocksTotalCount(((Double)data.get("stocks_total_count")).intValue());
            xgbQuoteChange.setSurgedLimitCount(((Double)data.get("surged_limit_count")).intValue());
            xgbQuoteChange.setSurgedNumCount(((Double)data.get("surged_num_count")).intValue());
            xgbQuoteChangeService.save(xgbQuoteChange);
        }
    }

    private static boolean validMultiStock(Map<String, Integer> titleIndex) {
        return titleIndex != null &&
                titleIndex.containsKey("symbol") &&
                titleIndex.containsKey("title") &&
                titleIndex.containsKey("reason") &&
                titleIndex.containsKey("cur_price") &&
                titleIndex.containsKey("px_change_rate") &&
                titleIndex.containsKey("turnover_ratio") &&
                titleIndex.containsKey("circulation_value") &&
                titleIndex.containsKey("total_value") &&
                titleIndex.containsKey("board_count") &&
                titleIndex.containsKey("continue_board_count") &&
                titleIndex.containsKey("board_days");
    }

    private static boolean validBoardPreday(Map<String, Integer> titleIndex) {
        return titleIndex != null &&
                titleIndex.containsKey("symbol") &&
                titleIndex.containsKey("title") &&
                titleIndex.containsKey("reason") &&
                titleIndex.containsKey("cur_price") &&
                titleIndex.containsKey("px_change_rate") &&
                titleIndex.containsKey("blockade_ratio") &&
                titleIndex.containsKey("turnover_ratio") &&
                titleIndex.containsKey("circulation_value") &&
                titleIndex.containsKey("total_value") &&
                titleIndex.containsKey("first_raise_time") &&
                titleIndex.containsKey("last_raise_time") &&
                titleIndex.containsKey("open_count") &&
                titleIndex.containsKey("continue_board_count") &&
                titleIndex.containsKey("bool_new_stock");
    }

    private static boolean validBoardDown(Map<String, Integer> titleIndex) {

        return titleIndex != null &&
                titleIndex.containsKey("symbol") &&
                titleIndex.containsKey("title") &&
                titleIndex.containsKey("reason") &&
                titleIndex.containsKey("cur_price") &&
                titleIndex.containsKey("px_change_rate") &&
                titleIndex.containsKey("blockade_ratio") &&
                titleIndex.containsKey("turnover_ratio") &&
                titleIndex.containsKey("circulation_value") &&
                titleIndex.containsKey("total_value") &&
                titleIndex.containsKey("first_board_time") &&
                titleIndex.containsKey("last_board_time") &&
                titleIndex.containsKey("open_count") &&
                titleIndex.containsKey("continue_board_count");
    }

    private static boolean validBoomStock(Map<String, Integer> titleIndex) {
        return titleIndex != null &&
                titleIndex.containsKey("symbol") &&
                titleIndex.containsKey("title") &&
                titleIndex.containsKey("reason") &&
                titleIndex.containsKey("cur_price") &&
                titleIndex.containsKey("px_change_rate") &&
                titleIndex.containsKey("turnover_ratio") &&
                titleIndex.containsKey("circulation_value") &&
                titleIndex.containsKey("total_value") &&
                titleIndex.containsKey("first_raise_time") &&
                titleIndex.containsKey("last_boom_time") &&
                titleIndex.containsKey("last_raise_time") &&
                titleIndex.containsKey("open_count") &&
                titleIndex.containsKey("continue_board_count");
    }

    private static boolean validBoardStock(Map<String, Integer> titleIndex) {
        return titleIndex != null &&
                titleIndex.containsKey("symbol") &&
                titleIndex.containsKey("title") &&
                titleIndex.containsKey("reason") &&
                titleIndex.containsKey("cur_price") &&
                titleIndex.containsKey("px_change_rate") &&
                titleIndex.containsKey("blockade_ratio") &&
                titleIndex.containsKey("turnover_ratio") &&
                titleIndex.containsKey("circulation_value") &&
                titleIndex.containsKey("total_value") &&
                titleIndex.containsKey("first_raise_time") &&
                titleIndex.containsKey("last_raise_time") &&
                titleIndex.containsKey("open_count") &&
                titleIndex.containsKey("continue_board_count") &&
                titleIndex.containsKey("bool_new_stock");
    }
}
